import React from 'react'
import BaseComponent from 'basecomponent'
import AntTable from 'anttable'
import apis from 'apis'
import {withRouter} from 'react-router'
import moment from 'moment'

const maxRecommend = Number.MAX_VALUE
class AddRecommend extends BaseComponent {
  constructor() {
    super(...arguments);
    const location = this.props.location;
    console.log(location)
    this.state={
      RecommendNumber: location.state ? location.state.num : -1,
      currentPage:1,
      pageSize:8,
      qualityData: [],
    }
    this.setNum = this.props.setNum
    this.addRecommend = this.addRecommend.bind(this)
  }

  componentDidMount() {
    this.getQualityList();
  }

  getQualityList() {
    this.qualityRP && this.qualityRP.request.abort()
    this.qualityRP = apis.quality.getQualityList('','')
    this.registerRequest(this.qualityRP.request)
    this.qualityRP.promise
      .then((res) => {
        let data = res.Data || []
        let qualityData = data.map((item) => {
          let updateTime = moment(new Date(item.UpdateTime)).format('YYYY-MM-DD HH:mm')
          return Object.assign({}, item, {UpdateTime: updateTime})
        })
        this.setState({qualityData: qualityData})
      })
      .catch((err) => {
        alert(err.msg)
      })
  }

  addRecommend(text,record){
    const {RecommendNumber} = this.state
    if(RecommendNumber === -1) {
      alert('添加出错')
      return
    }
    if(RecommendNumber >= maxRecommend) {
      alert('最多有'+ maxRecommend +'个推荐')
      return
    }
    this.postRecommendRP && this.postRecommendRP.request.abort()
    this.postRecommendRP = apis.recommend.postRecommend(2, {Id: record.Id});
    this.registerRequest(this.postRecommendRP.request);
    this.postRecommendRP.promise
      .then((res)=>{
        alert('添加成功')
        this.setNum(RecommendNumber + 1)
        this.setState({RecommendNumber: RecommendNumber + 1})
      })
      .catch((err)=>{
        alert('添加出错')
      })
  }

  render() {
    const {qualityData,currentPage,pageSize,} = this.state;
    const columns = [{
      title: '编号',
      dataIndex: 'Id',
      key: 'Id',
    }, {
      title: '名称',
      dataIndex: 'Name',
      key: 'Name'
    },
      {
        title: '播放量',
        dataIndex: 'TotalPlayTimes',
        key: 'TotalPlayTimes'
      },
      {
        title: '时长',
        dataIndex: 'TotalDuration',
        key: 'TotalDuration'
      },
      {
        title: '上传时间',
        dataIndex: 'UpdateTime',
        key: 'UpdateTime',
      },
      {
        title: '操作',
        key: 'action',
        render: (text, record) => (
          <span>
          <a onClick={()=>{this.addRecommend(text,record)}}>
            添加
          </a>
        </span>
        ),
      }];

    return (
      <AntTable columns={columns} data={qualityData}
                pagination={true} currentPage={currentPage} pageSize={pageSize}
                onChange={(pagination)=>{
                  this.setState({
                    currentPage:pagination.current,
                    pageSize:pagination.pageSize,
                  })
                }}/>
    );
  }
}

module.exports = withRouter(AddRecommend);
